//
//  CXLabel.swift
//  xingluoluo
//
//  Created by Mac Book on 2019/2/19.
//  Copyright © 2019 李旭峰. All rights reserved.
//

import UIKit

class CXLabel: UILabel {
    var openShadow:Bool = true
    private var padding = UIEdgeInsets.zero
    
    var isNeedBorder:Bool = false
    
    @IBInspectable var paddingLeft:CGFloat {
        get { return padding.left }
        set { padding.left = newValue }
    }
    @IBInspectable var paddingRight:CGFloat {
        get { return padding.right }
        set { padding.right = newValue }
    }
    @IBInspectable var paddingTop:CGFloat {
        get { return padding.top }
        set { padding.top = newValue }
    }
    @IBInspectable var paddingBottom:CGFloat {
        get { return padding.bottom }
        set { padding.bottom = newValue }
    }
    @IBInspectable var paddingValue:UIEdgeInsets {
        get{ return padding }
        set{ padding = newValue }
    }
    @IBInspectable var cxTextColor:UIColor = UIColor.clear {
        didSet{
            DispatchQueue.main.async {
                self.configShadow()
            }
        }
    }
    @IBInspectable var cxFont:UIFont = FontAsset.PingFangSC_Regular.size(12.0){
        didSet{
            DispatchQueue.main.async {
                self.configShadow()
            }
        }
    }
    @IBInspectable var cxText:String?{
        didSet{
            DispatchQueue.main.async {
                self.configShadow()
            }
        }
    }
    func configShadow(){
        let text = self.cxText ?? ""
        let shadow = NSShadow()
        shadow.shadowColor = UIColor(hexStr: "#000000", alpha: 0.5)
        shadow.shadowBlurRadius = 3.0
        shadow.shadowOffset = CGSize(width: 0.0, height: 0.0)
        
        var attributes:[NSAttributedString.Key:Any] = [:]
        attributes[.font] = self.cxFont
        attributes[.foregroundColor] = self.cxTextColor
        if self.openShadow {
            attributes[.shadow] = shadow
            attributes[.verticalGlyphForm] = 0
        }
        self.attributedText = NSAttributedString(string: text, attributes: attributes)
    }
    func textSize(limitWidth:CGFloat,limitHeight:CGFloat) -> CGSize{
        let tempWidth = limitWidth - self.padding.left - self.padding.right
        let tempHeight = limitHeight - self.padding.top - self.padding.bottom
        let tempSize = (self.cxText! as NSString).boundingRect(with: CGSize(width: tempWidth, height: tempHeight), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font : self.font!], context: nil)
        let realWidth = tempSize.width + self.padding.left + self.padding.right
        let realHeight = tempSize.height + self.padding.top + self.padding.bottom
        return CGSize(width: realWidth, height: realHeight)
    }
    override func drawText(in rect: CGRect) {
        super.drawText(in: rect.inset(by: padding))
//        if self.isNeedBorder {
//            let radius = max(rect.width, rect.height) * 0.5
//            self.layer.cornerRadius = radius
//            self.layer.borderWidth = 1.0
//            self.layer.borderColor = UIColor(hexStr: "#CECECE", alpha: 1.0).cgColor
//        }else{
//            self.layer.cornerRadius = 0.0
//            self.layer.borderWidth = 0.0
//            self.layer.borderColor = UIColor.clear.cgColor
//        }
    }
    override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        let inserts = self.padding
        var rect = super.textRect(forBounds: bounds.inset(by: inserts), limitedToNumberOfLines: numberOfLines)
        rect.origin.x -= inserts.left
        rect.origin.y -= inserts.top
        rect.size.width += (inserts.left + inserts.right)
        rect.size.height += (inserts.top + inserts.bottom)
        return rect
    }
}
extension CXLabel {
    static func TextLabel(fontSize:CGFloat,textColor:UIColor) -> CXLabel {
        let label = CXLabel()
        label.cxFont = FontAsset.PingFangSC_Regular.size(fontSize)
        label.cxTextColor = textColor
        label.textAlignment = .center
        return label
    }
    static func IconLabel(fontSize:CGFloat,textColor:UIColor) -> CXLabel {
        let label = CXLabel()
        // TODO: 
        label.cxFont = FontAsset.PingFangSC_Regular.size(fontSize)
//        label.cxFont = FontAsset.Icon.size(fontSize)
        label.cxTextColor = textColor
        label.textAlignment = .center
        return label
    }
}
